app.service('chargeRuleService',['$q','utilService',function($q,utilService){

    //删除会员卡
    this.deleteRule=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'charge/rule/del',
            data:{id:$scope.id},
            method:'DELETE'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.addRule=function($scope){

        if($scope.chargeRuleVO.isGiveCoupons==0){
            delete $scope.chargeRuleVO.giveCouponsId;
        }

        if($scope.chargeRuleVO.isGiveIntegral==0){
            delete $scope.chargeRuleVO.giveIntegral;
        }

        if($scope.chargeRuleVO.isGiveMoney==0){
            delete $scope.chargeRuleVO.giveMoney;
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'charge/rule/add',
            data:$scope.chargeRuleVO,
            method:'POST'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.updateRule=function($scope){

        if($scope.chargeRuleVO.isGiveCoupons==0){
            delete $scope.chargeRuleVO.giveCouponsId;
        }

        if($scope.chargeRuleVO.giveIntegral==null){
            $scope.chargeRuleVO.giveIntegral='';
        }

        if($scope.chargeRuleVO.isGiveMoney==null){
            $scope.chargeRuleVO.giveMoney='';
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'charge/rule/update',
            data:$scope.chargeRuleVO,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.findCoupons=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'coupons/list',
            params:$scope.coupons
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };

    this.findById=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'charge/rule/find',
            params:{id:$scope.chargeRuleVO.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };
}]);